package com.codeoss.framework.webide.app.service.impl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.data.metadata.Query;
import com.codeoss.framework.data.metadata.Restrictions;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.impexp.ExportManager;
import com.codeoss.framework.impexp.ImportManager;
import com.codeoss.framework.webide.app.service.CommonAppDataHandler;
import com.codeoss.module.ir.model.IrApplication;
import com.codeoss.module.ir.model.IrMenu;
import com.codeoss.module.ir.service.IIrMenuService;

@Service("ir_menuAppDataHandler")
public class IrMenuAppDataHandler extends CommonAppDataHandler {
    @Autowired
    private IIrMenuService irMenuService;
    @Autowired
    private ExportManager exportManager;
    @Autowired
    private ITableStore tableStore;
    @Autowired
  	private ImportManager importManager;
	@Override
	public void dataExport(YunContext yunContext, IrApplication irApplication, String tableName) throws IOException{
		// TODO Auto-generated method stub
		QueryWrapper<IrMenu> queryWrapper=new QueryWrapper<IrMenu>();
		queryWrapper.in("module_type", getModuleTypeList(yunContext, irApplication));
		List<IrMenu> irMenuList=irMenuService.list(queryWrapper);
		Map<Long,Object> processedMap=new HashMap<Long,Object>();
		int index=0;
		for (int i = 0; i < irMenuList.size(); i++) {
			IrMenu irMenu=irMenuList.get(i);
			List<Long> idList=new ArrayList<Long>();
			idList.add(irMenu.getId());
			while(irMenu.getParentId()!=null) {
				Long parentId=irMenu.getParentId();
				IrMenu parentMenu=getIrMenuById(irMenuList, parentId);
				if(parentMenu==null) {
					break;
				}
				idList.add(0,parentId);
				irMenu=getIrMenuById(irMenuList,parentId);
			}
			idList.add(0,irMenu.getId());
			for(int j=0;j<idList.size();j++) {
				Long id=idList.get(j);
				if(processedMap.get(id)!=null) {
					continue;
				}
				irMenu=getIrMenuById(irMenuList, id);
				Table table=tableStore.get("ir_menu");
				Query query=new Query();
				query.add(Restrictions.eq("id", irMenu.getId()));
				String filename="ir_menu"+index;
				writeConfigKeyValue(yunContext, "ir_menu", filename);
				FileOutputStream os=createFileOutputStream(yunContext, filename);
				exportManager.exportProperties(yunContext, table, query, os);
				os.close();
				processedMap.put(id, true);
				index++;
			}
		}
	}

	private IrMenu getIrMenuById(List<IrMenu> irMenuList,Long id) {
		for(int i=0;i<irMenuList.size();i++) {
			if(irMenuList.get(i).getId().equals(id)) {
				return irMenuList.get(i);
			}
		}
		return null;
	}
	
	@Override
	@Transactional
	public void dataImport(YunContext yunContext, IrApplication irApplication, String tableName, List<String> filenameList)throws IOException {
		// TODO Auto-generated method stub
		if (filenameList == null) {
			return;
		}
		for(int i=0;i<filenameList.size();i++) {
			String filename=filenameList.get(i);
			InputStream is=createFileInputStream(yunContext, filename);
			Table table = tableStore.get("ir_menu");
			importManager.importProperties(yunContext, table, is);
			is.close();
		}
	}

}
